<template>
  <div class="home">
    <header>
      商品列表
    </header>
    <section>
      <span v-for="(item,index) in list" :class="{active:checkIndex==index}" :key="index" @click="checkIndex = index">{{item.name}}</span>
    </section>
    <ul>
      <li v-for="(item,index) in list[checkIndex]?.child">
         <img src="../assets/logo.png" alt="">
         <div>
           <h4>{{item.name}}</h4>
           <p class="price">￥{{item.price}}</p>
           <p>
             <span>{{item.payNum}}人付款</span>
             <button @click="addcart(item)">+</button>
           </p>
         </div>
      </li>
    </ul>
  </div>
</template>

<script>
// @ is an alias to /src
import { mapActions,mapState,mapMutations } from 'vuex'
export default {
  name: 'Home',
  components: {

  },
  data(){
    return{
      checkIndex: 0
    }
  },
  computed:{
    ...mapState(['list'])
  },
  created() {
    this.getList();
  },
  methods: {
    ...mapMutations(['saveCart']),
    ...mapActions(['getList']),
    addcart(val){ // 加入购物车和跳转的方法
       this.saveCart(val);
       this.$router.push('/about')
    }
  },
}
</script>
<style lang="scss" scoped>
  .home{
    height: 100%;
    background: #e9e9e9;
  }
  header{
    line-height: 45px;
    text-align: center;
  }
  section{
    display: flex;
    height: 50px;
    align-items: center;
    color: #fff;
    background: #440f7d;
    span{
      width: 20%;
      text-align: center;
      line-height: 50px;
    }
    .active{
      background: #8d3ec5;
    }
  }
  li{
    display: flex;
    padding: 10px;
    margin-bottom: 10px;
    background: #fff;
    img{
      width: 100px;
    }
    div{
      flex: 1;
      .price{
        color: #8d3ec5;
        margin: 10px 0;
      }
      button{
        float: right;
        border: none;
        background: #8d3ec5;
        color: #fff;
        padding: 2px 10px;
        border-radius: 10px;
      }
    }
  }
</style>
